<?php

class m121022_143222_create_printing extends CDbMigration {

	protected $_tables = array(
		'printing'=>array(
			'_id'=>'pk',
			'insertTime'=>'integer',
			'updateTime'=>'integer',
			'province'=>'string',
			'sector'=>'string',
			'note'=>'text',
			'status'=>'string',
		),
	);
	protected $_indexes = array(
		'printing'=>array(
			'province',
			'sector',
			'status',
		),
	);

	public function safeUp() {
		foreach ($this->_tables as $key=>$value) {
			$this->createTable($key, $value);
		}

		foreach ($this->_indexes as $table=>$columns) {
			foreach ($columns as $column) {
				$name = 'idx_' . $table . '_' . $column;
				$this->createIndex($name, $table, $column);
			}
		}

		$auth = Yii::app()->authManager;

		// Print Operation
		$auth->createOperation('printingList', 'List printing');
		$auth->createOperation('printingView', 'View printing');
		$auth->createOperation('printingCreate', 'Create printing');
		$auth->createOperation('printingUpdate', 'Update printing');
		$auth->createOperation('printingDelete', 'Delete printing');
		$auth->createOperation('printingManage', 'Manage printing');

		// Print Moderator
		$role = $auth->createRole('printingModerator', 'Censor Moderator');
		$role->addChild('printingList');
		$role->addChild('printingView');
		$role->addChild('printingCreate');
		$role->addChild('printingUpdate');
		$role->addChild('printingDelete');
		$role->addChild('printingManage');

		$auth->addItemChild('admin', 'printingModerator');
	}

	public function safeDown() {
		foreach ($this->_indexes as $table=>$columns) {
			foreach ($columns as $column) {
				$name = 'idx_' . $table . '_' . $column;
				$this->dropIndex($name, $table, $column);
			}
		}

		foreach ($this->_tables as $key=>$value) {
			$this->dropTable($key);
		}

		$auth = Yii::app()->authManager;

		$auth->removeItemChild('admin', 'printingModerator');

		$auth->removeItemChild('printingModerator', 'printingList');
		$auth->removeItemChild('printingModerator', 'printingView');
		$auth->removeItemChild('printingModerator', 'printingCreate');
		$auth->removeItemChild('printingModerator', 'printingUpdate');
		$auth->removeItemChild('printingModerator', 'printingDelete');
		$auth->removeItemChild('printingModerator', 'printingManage');

		$auth->removeAuthItem('printingList');
		$auth->removeAuthItem('printingView');
		$auth->removeAuthItem('printingCreate');
		$auth->removeAuthItem('printingUpdate');
		$auth->removeAuthItem('printingDelete');
		$auth->removeAuthItem('printingManage');

		$auth->removeAuthItem('printingModerator');
	}

}